package zengjin.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import zengjin.common.PageResponse;
import zengjin.mapper.BulletinMapper;
import zengjin.model.dto.bulletin.AddBulletinRequest;
import zengjin.model.dto.bulletin.PageBulletinRequest;
import zengjin.model.dto.bulletin.UpdateBulletinRequest;
import zengjin.model.en.Bulletin;
import zengjin.model.vo.bulletin.BulletinVO;
import zengjin.service.BulletinService;

/**
 * @author 19324
 * @description 针对表【bulletin】的数据库操作Service实现
 * @createDate 2025-03-08 13:12:59
 */
@Service
public class BulletinServiceImpl extends ServiceImpl<BulletinMapper, Bulletin>
        implements BulletinService {

    @Resource
    private BulletinMapper bulletinMapper;

    @Override
    public Boolean addBulletin(AddBulletinRequest addBulletinRequest) {
        //1.获取参数
        String bulletinStatus = addBulletinRequest.getBulletinStatus();
        String bulletinTitle = addBulletinRequest.getBulletinTitle();
        String bulletinText = addBulletinRequest.getBulletinText();
        String bulletinCreateName = addBulletinRequest.getBulletinCreateName();

        //2.封装对象
        Bulletin bulletin = new Bulletin();
        bulletin.setBulletinStatus(bulletinStatus);
        bulletin.setBulletinTitle(bulletinTitle);
        bulletin.setBulletinText(bulletinText);
        bulletin.setBulletinCreateName(bulletinCreateName);

        //3.调用mapper
        int insert = bulletinMapper.insert(bulletin);
        return insert > 0;
    }

    @Override
    public Boolean updateBulletin(UpdateBulletinRequest updateBulletinRequest) {
        //1.获取参数
        String bulletinStatus = updateBulletinRequest.getBulletinStatus();
        String bulletinTitle = updateBulletinRequest.getBulletinTitle();
        String bulletinText = updateBulletinRequest.getBulletinText();

        //2.调用mapper
        UpdateWrapper<Bulletin> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("bulletin_id", updateBulletinRequest.getBulletinId())
                .set("bulletin_status", bulletinStatus)
                .set("bulletin_title", bulletinTitle)
                .set("bulletin_text", bulletinText);
        int update = bulletinMapper.update(null, updateWrapper);
        return update > 0;

    }

    @Override
    public PageResponse<BulletinVO> pageBulletin(PageBulletinRequest pageBulletinRequest) {
        //1.获取参数
        String bulletinStatus = pageBulletinRequest.getBulletinStatus();
        String bulletinTitle = pageBulletinRequest.getBulletinTitle();

        //2.构建分页条件
        Page<Bulletin> page = pageBulletinRequest.toPageDefaultSortByUpdateTime();
        Page<Bulletin> bulletinPage = lambdaQuery()
                .eq(StrUtil.isNotBlank(bulletinStatus), Bulletin::getBulletinStatus, bulletinStatus)
                .like(StrUtil.isNotBlank(bulletinTitle), Bulletin::getBulletinTitle, bulletinTitle)
                .page(page);
        return PageResponse.of(bulletinPage, BulletinVO.class);
    }
}




